package com.wuzhi.shop.product.web;

import com.wuzhi.shop.product.entity.CategoryEntity;
import com.wuzhi.shop.product.service.CategoryService;
import com.wuzhi.shop.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * web:
 * 1. 所有前端的请求。后端的控制器放在app包中
 */
@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    /**
     * Model:
     * 1. 将请求的数据传给html
     *
     * @return
     */
    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        //        TODO 查出所有的1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();


        model.addAttribute("categories", categoryEntities);
        //        用视图解析器进行拼串：加上页面的路径和后缀 .html
        return "index";
    }

    /**
     * 如果返回json数据，带上标签：@ResponseBody
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }
}
